<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MeetingController extends Controller
{

	//会议列表
	public function getIndex(Request $request)
	{
		if($request['name']){	
			$meeinfo = DB::table('meetingroom')
			 ->where('MeetingRoomName','like','%'.$request['name'].'%')
            ->paginate(10);
           }else{
           	$meeinfo = DB::table('meetingroom')
           	->paginate(3);
           } 
           $info = $request -> all();
			// dd($meeinfo);
		return view('Lzk.MeetingRoom.Meeting',['meeinfo'=>$meeinfo,'info'=>$info]);
	} 

	//会议室添加
	public function getAdd()
	{
		return view('Lzk.MeetingRoom.AddMeeting');
	}

	//执行添加
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$data = $request->only('MeetingRoomName');
		// dd($data);
		$ins = DB::table('meetingroom')->insert($data);
		if($ins){
			return redirect('/MeetingRoom');
		}else{
			return back();
		}
	}

	public function getEdit($id)
	{
		$res = DB::table('meetingroom')->where('id',$id)->first();
		$info = DB::select("select MeetingRoomID from meetingroomreservation where MeetingRoomID = {$id}");
		if($info){
			echo '<script>alert("此会议是有预约,不能修改");window.location.href="/MeetingRoom";</script>';
			die;
		}
		// dd($res);
		return view('Lzk.MeetingRoom.EditMeeting',['res'=>$res]);
	}

	public function postUpdate(Request $request)
	{
		// dd($request->all());
		$id = $request->only('id');
		$info = $request->only('MeetingRoomName');
		$res = DB::table('meetingroom')->where('id',$id)->update($info);
		if($res){
			return redirect('/MeetingRoom');
		}else{
			return back();
		}
	}

	//会议室删除
	public function getDel($id)
	{
		// echo $id;
		$del = DB::select("select MeetingRoomID from meetingroomreservation where MeetingRoomID = {$id}");
		if($del){
        	echo '<script>alert("此会议是有预约,不能删除");window.location.href="/MeetingRoom";</script>';
        }else{
        	DB::table('meetingroom')->delete($id);
        	return redirect('/MeetingRoom');
        }
	}

	
}
